package com.eos.inter.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.hf.utils.DESPlus;
import com.hf.utils.StringUtil;

/**
 * 接口代码Servlet的基类
 * 
 * @author Administrator
 * 
 */
public class BaseHttpServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	public Logger log = Logger.getLogger("log4j");
	private DESPlus desPlus = new DESPlus();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	}

	/**
	 * 根据通讯协议要求 不能为空的参数要做判空操作
	 * 
	 * @param parameters不确定参数的个数
	 * @return 有为空返回true
	 */
	public boolean identification(String... parameters) {
		if (null == parameters) {
			return true;
		}
		for (String parameter : parameters) {
			if (StringUtil.isEmpty(parameter)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 验证若干个参数是否均为整数
	 * 
	 * @param parameters
	 * @return 均为整数则true 至少有一个不是整数则false
	 */
	public boolean isNumberParameters(String... parameters) {
		if (null == parameters)
			return false;
		for (String parameter : parameters) {
			if (!StringUtil.isNumber(parameter)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 解析HTTP BODY域里面的数据
	 * 
	 * @param request
	 * @return
	 */
	public Map<String, String> parseBodyPost(HttpServletRequest request) {
		String procotol_id = StringUtil.NullToString(request
				.getParameter("p_id"));
		Map<String, String> bodyMap = new HashMap<String, String>();
		String inputLine = "";
		StringBuffer sb = new StringBuffer(128);
		BufferedReader br = null;
		try {
			br = request.getReader();
			while ((inputLine = br.readLine()) != null) {
				sb.append(inputLine);
			}
		} catch (IOException e) {
			System.out.println("IOException: " + e);
		} finally {
			inputLine = null;
			if (null != br)
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		String body = "";
		// 加密协议ID为1
		if ("1".equals(procotol_id)) {
			body = StringUtil.NullToString(desPlus.decrypt(sb.toString()));// 获取解密后的数据
		}
		{
			body = StringUtil.replace(StringUtil.replace(body, "[", ""), "]",
					"");// 去除[]
			String[] body_1 = StringUtil.split(body, ",");// 利用,分割字符串
			if (null != body_1) {
				for (String temp : body_1) {
					String[] body_2 = StringUtil.split(temp, ":");// 利用:分割字符串
					if (null != body_2 && body_2.length > 1) {
						bodyMap.put(StringUtil.NullToString(body_2[0]),
								StringUtil.NullToString(body_2[1]));
					}
					body_2 = null;
				}
				body_1 = null;
			}
		}
		body = null;
		sb = null;
		return bodyMap;
	}

	/**
	 * 解析HTTP HEAD域中的所有键值对
	 * 
	 * @param request
	 * @return map
	 */
	public Map<String, String> parseHeadString(HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		@SuppressWarnings("unchecked")
		Enumeration<String> heads = (Enumeration<String>) request
				.getHeaderNames();
		while (heads.hasMoreElements()) {
			String head = heads.nextElement();
			map.put(head, StringUtil.NullToString(request.getHeader(head)));
			head = null;
		}
		heads = null;
		return map;
	}

	/**
	 * 获取HTTP HEAD域里面某个head的参数值
	 * 
	 * @param request
	 * @return
	 */
	public String getHeadValue(HttpServletRequest request, String head) {
		Map<String, String> map = parseHeadString(request);
		if (null != map) {
			return map.get(head);
		}
		return "";
	}

	/**
	 * 获取用户联网的IP地址
	 * 
	 * @param request
	 * @return
	 */
	public String getRemoteHost(HttpServletRequest request) {
		return request.getRemoteHost();
	}
}
